package io.renren.modules.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.back.entity.ShopUserEntity;
import io.renren.modules.back.service.ShopUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 普通用户
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2018年12月8日 上午10:40:10
 */
@RestController
@RequestMapping("/back/shopUser")
public class ShopUserController {
    @Autowired
    private ShopUserService shopUserService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("shopUser:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ShopUserEntity> userList = shopUserService.queryList(query);
        int total = shopUserService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("shopUser:info")
    public R info(@PathVariable("id") Long id) {
        ShopUserEntity user = shopUserService.getById(id);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("shopUser:save")
    public R save(@RequestBody ShopUserEntity shopUser) {
        ValidatorUtils.validateEntity(shopUser, AddGroup.class);

        List<ShopUserEntity> shopUserEntity = shopUserService.list(
            new QueryWrapper<ShopUserEntity>().eq("mobile", shopUser.getMobile())
        );

        if ( !StringUtil.isNullOrEmpty(shopUserEntity) ) {
            return R.error("手机号码已被注册");
        }
        shopUser.setCreateTime(new Date());
        shopUser.setPassword(DigestUtils.sha256Hex("88888888"));
        shopUserService.save(shopUser);
        return R.ok("执行成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("shopUser:update")
    public R update(@RequestBody ShopUserEntity shopUser) {
        ValidatorUtils.validateEntity(shopUser, UpdateGroup.class);
        ShopUserEntity shopUserEntity = shopUserService.getById(shopUser.getUserId());
        if (StringUtil.isNullOrEmpty(shopUserEntity)) { return R.error("账户不存在"); }
        if (!shopUserEntity.getMobile().equals(shopUser.getMobile())) {
            List<ShopUserEntity> shopUserEntitys = shopUserService.list(
                new QueryWrapper<ShopUserEntity>().eq("mobile", shopUser.getMobile())
            );
            if (!StringUtil.isNullOrEmpty(shopUserEntitys)) {
                return R.error("手机号码已被注册");
            }
        }
        shopUserService.updateById(shopUser);
        return R.ok("执行成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("shopUser:delete")
    public R delete(@RequestBody Long[] ids) {
        shopUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 搜索
     */
    @RequestMapping("/search/{keyword}")
    @RequiresPermissions("shopUser:list")
    public R search(@PathVariable("keyword") String keyword) {
        List<ShopUserEntity> shopUsers = shopUserService.queryByKeyword(keyword);
        return R.ok().put("shopUsers", shopUsers);
    }
}

